Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
made a function to find the max between 3 numbers but returns " 0"

so this is what i did, if anyone has some free time maybe you could see what i did wrong? i cant seem to figure it out and i'm very curious :c

let mayor = 0;

function max(a, b, c) {
  for (let i = 0; i <= max.length; i++) {
    if (max[i] > mayor) {
      mayor = max[i];
    }
    return mayor;
  }
}

let mayorFin = max(5, 2, 6);

console.log(mayorFin); // 6 // 6

it returns the 0 so it never changes mayor to the max in between parenthesis.

I'm in the process of learning and i believe there's no such thing as a dumb question, any help is much appreciated form the bottom of my heart cause' your time is valuable, thanks!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

max is not an array. create an array inside function and use it. like this:

let mayor = 0;

function max(a, b, c) {
  let arr = [a, b, c]

  for (let i = 0; i <= arr.length; i++) {

    if (arr[i] > mayor) {

      mayor = arr[i];

    }
  }
  return mayor;

}

let mayorFin = max(5, 2, 6);

console.log(mayorFin); // 6 // 6

about 4 years ago · Juan Pablo Isaza Report

0

You may use "..." operator like this. It allow you insert any amount of arguments into your function.

function max(...args) {
  let mayor = 0;

  for (let arg of args) {
    if (arg > mayor) mayor = arg;
  }
  return mayor;
}

let mayorFin = max(5, 2, 6);

console.log(mayorFin); // 6 // 6

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!